package com.nft.admin.collection.controller;

import com.nft.collection.service.BankCardManagementService;
import com.nft.common.utils.IdUtils;
import com.nft.common.utils.Validator;
import com.nft.common.vo.Result;
import com.nft.member.domain.BankCardManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bankCardMean")
public class BankCardManagementController {
    @Autowired
    private BankCardManagementService bankCardManagementService;

    @PostMapping("/addBankManage")
    @ResponseBody
    public Result<String> addBankManage(@RequestBody BankCardManagement bankCardManagement) {
        List<BankCardManagement> bankCardManagements = bankCardManagementService.queryBackInfo();
        if(!bankCardManagements.isEmpty()){
            return Result.fail("已经存在银行卡信息，请先删除或修改");
        }
        boolean validPhoneNumber = Validator.isValidPhoneNumber(bankCardManagement.getHolderPhone());
        if(!validPhoneNumber){
            return Result.fail("手机号不正确");
        }
        boolean validBankCardNumber = Validator.isValidBankCardNumber(bankCardManagement.getBankCardNumber());
        if(!validBankCardNumber){
            return Result.fail("银行卡号不正确，请仔细确认");
        }


        bankCardManagement.setStatus("0");
        bankCardManagement.setCreateTime(new Date());
        bankCardManagement.setId(IdUtils.getId());
        bankCardManagementService.addBankManage(bankCardManagement);
        return Result.success();
    }

    /**
     * 查询
     */
    @GetMapping("queryBackInfo")
    public Result<List<BankCardManagement>> queryBackInfo() {
        List<BankCardManagement> bankCardManagement =
                bankCardManagementService.queryBackInfo();
        return Result.success(bankCardManagement);
    }

    /**
     * 根据id 查询
     */
    @GetMapping("queryBackInfoById")
    public Result<BankCardManagement> queryBackInfoById(@RequestParam String id) {
        BankCardManagement bankCardManagement =
                bankCardManagementService.queryBackInfoById(id);
        return Result.success(bankCardManagement);
    }
    /**
     * 修改
     */
    @PostMapping("updateBackInfo")
    public Result<BankCardManagement> updateBackInfo(@RequestBody BankCardManagement bankCardManagement){
        bankCardManagement.setUpdateTime(new Date());
        bankCardManagementService.updateBackInfo(bankCardManagement);
        return Result.success(bankCardManagement);
    }

    /**
     * 删除
     */
    @GetMapping("deleteBackInfo")
    public Result<String> deleteBackInfo(@RequestParam String id){
        bankCardManagementService.deleteBackInfo(id);
        return Result.success();
    }

}
